<?php

namespace app\model;

use think\Model;

class Pig extends Model
{
    const IS_DELETE_NO = 0; // 未删
    const IS_EXIT_NO = 0; //未出栏

    const STATUS_HEALTH = 10; // 健康
    const STATUS_POTENTIAL_HEALTH = 11; // 疑似健康
    const STATUS_SICK = 15; // 生病
    const STATUS_OBSERVATION = 20;// 待观察
    const STATUS_INEFFECTIVE = 98; // 治疗无效
    const STATUS_DEAD = 99; //已死亡
    const STATUS_SHIPPED = 100; //已出栏

    /**
     * @Description 判断第三方主键是否存在
     * @author: zj
     * @param int $factoryId
     * @param int $outId
     * @time 2024/03/11 17:36:12
     * @return false|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkOutIdIsExist(int $factoryId, int $outId)
    {
        $info = $this->field('id')
            ->where('factory_id',$factoryId)
            ->where('out_id',$outId)
            ->where('delete_time',self::IS_DELETE_NO)
            ->find();
        if (!$info) {
            return false;
        }

        return $info['id'];
    }

    /**
     * @Description 统计在栏猪的数量
     * @author: zj
     * @param int $factoryId
     * @param int $barnID
     * @param int $fenceId
     * @param int $status
     * @param int $start_time
     * @param int $end_time
     * @time 2024/03/18 18:28:40
     * @return int
     */
    public function checkPigCount(int $factoryId,int $barnID = 0,int $fenceId = 0,int $status = 0,int $start_time = 0,int $end_time = 0)
    {
        $query = $this
            ->where('factory_id',$factoryId)
            ->where('exit_time',self::IS_EXIT_NO)
            ->where('delete_time',self::IS_DELETE_NO);
        if ($barnID) {
            $query->where('out_barn_id',$barnID);
        }
        if ($fenceId) {
            $query->where('out_fence_id',$fenceId);
        }
        if ($status > 0) {
            $query->where('health',$status);
        }
        if ($start_time) {
            $query->where('record_time','>=',$start_time);
        }
        if ($end_time) {
            $query->where('record_time','<=',$end_time);
        }
        return $query->count() ?? 0;
    }

    /**
     * @Description 猪的总体重
     * @author: zj
     * @param int $factoryId
     * @param int $barnId
     * @param int $fenceId
     * @param int $status
     * @time 2024/03/20 14:49:26
     * @return int
     */
    public function pigWeightTotal(int $factoryId,int $barnId = 0,int $fenceId = 0,int $status = 0)
    {
        $query = $this->alias('p')
            ->leftJoin('fence fe','fe.out_id = p.out_fence_id')
            ->leftJoin('barn b','b.out_id = fe.out_barn_id')
            ->where([
                ['p.factory_id','=',$factoryId],
                ['p.delete_time','=',self::IS_DELETE_NO],
                ['p.exit_time','=',self::IS_EXIT_NO]
            ]);
        if ($barnId) {
            $query->where('b.out_id',$barnId);
        }
        if ($fenceId) {
            $query->where('p.out_fence_id',$fenceId);
        }
        if ($status) {
            $query->where('p.health',$status);
        }

        return $query->sum('p.weight') ?? 0;
    }

}